package com.youlai.boot.system.controller;

import com.youlai.boot.common.result.Result;
import com.youlai.boot.common.result.ResultCode;
import com.youlai.boot.system.handler.DuplicateDeviceNumberException;
import com.youlai.boot.system.model.QB.Device;
import com.youlai.boot.system.model.QB.DeviceInfoVO;
import com.youlai.boot.system.service.DeviceService;
import com.youlai.boot.system.service.impl.DeviceServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "设备模块")
@RestController
@RequestMapping("/api/v1/device")
@RequiredArgsConstructor
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    private static final Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class);
    //添加设备
    @Operation(summary = "添加设备以及添加设备使用记录")
    @PostMapping("/dev")
    public Result addDevice( @Valid @RequestBody Device device) {
        return Result.judge(deviceService.addDevice(device), "添加设备成功","添加设备失败");
    }


/*
设置设备状态
 */
    @PostMapping("/{deviceNumber}/status")
    public Result updateDeviceStatus(@PathVariable("deviceNumber") String deviceNumber, @RequestParam int newStatus) {
        Result result = new Result();

        boolean success = deviceService.updateDeviceStatus(deviceNumber, newStatus);
        if (success) {
            result.setSuccess(true);
            result.setMessage("设备状态修改成功");
        } else {
            result.setSuccess(false);
            result.setMessage("设备修改失败");
        }
        return result;
    }
    //查询所有设备
    @PostMapping("/all")
    public Result<List<Device>> getAllDevice(){
        List<Device> devices = deviceService.getAllDevice();
//        if (devices.isEmpty()){
//            throw new RuntimeException("查询失败，没有找到任何设备");
//        }
//        return (Result<Device>) devices;
      Result<List<Device>> result;
      if (!devices.isEmpty()){
          result =  Result.success(devices);
          result.setMessage("查询设备成功");
          result.setCode(ResultCode.SUCCESS.getCode());
      }else {
          result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR);
          result.setMessage("查询设备失败，未找到设备");
      }
      return result;
    }
    //根据编号查询设备
@GetMapping("/{devicenumber}/number")
    public  Result<Device> getDeviceByNumber(@PathVariable("devicenumber") String devicenumber){
        Device device = deviceService.getDeviceByNumber(devicenumber);
        if (device !=null) {
            return Result.success(device);
        }
        else {
            return Result.failed("未找到相关编号" + devicenumber + "的设备");
        }
    }
    //根据编号查询设备使用记录
    @GetMapping("/info")
    public List<DeviceInfoVO> getDeviceInfo(@RequestParam("devicenumber") String devicenumber){
    logger.info("接收到的设备编号参数：{}", devicenumber);
        List<DeviceInfoVO> result = deviceService.getDeviceInfo(devicenumber);
        logger.info("查询结果：{}", result);

        return result;

    }

    /**
     * 修改设备
     * @param device
     * @return
     */
    @PutMapping("/upd")
    public  Result<?> updateDevice(@RequestBody Device device){
        return Result.judge(deviceService.updateDevice(device),"修改成功","修改失败");
    }

    @DeleteMapping("/{id}")
    public  Result<?> deleteHygiene( @PathVariable Integer id){
        return Result.judge(deviceService.deleteDeviceById(id),"删除成功","删除失败");
    }


//    @PostMapping("/{id}/usage")
//    public boolean addDeviceUsageRecord(@PathVariable("id") Long id, Device device) {
//        if (deviceService.addDeviceUsageRecord(id)) {
//            // 如果id存在，可以选择更新操作或者返回错误信息
//            return false;
//        } else {
//            // 如果id不存在，进行插入操作
//            device.setId(id);
//            return deviceService.addDeviceUsageRecord(device);
//        }
//    }
//    @PostMapping("/{id}/usage")
//    public  boolean addDeviceUsageRecord(@PathVariable("id") Long id,
//                                         String usagePurpose,
//                                         LocalDateTime usageTime){
//        return  deviceService.addDeviceUsageRecordById(id,usagePurpose,usageTime);
//    }

}

